<?php
namespace app\index\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
use think\Cache;
use traits\model\SoftDelete;

class Tag extends CommonController
{
    /**
     * 标签管理
     * @return mixed
     */
    public function index()
    {
        $db_tags = DB::name('tag_sort');
        $list = $db_tags->select();
//        detail 数据
        for($i=0;$i<count($list);$i++){
            $map['tag_id'] = $list[$i]['name'];
            $list[$i]['detail'] =DB::name('tag_detail')
                ->where($map)
                ->select();
        }

        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 增加
     */
    public function add() {
        $db_tags = DB::name('tag_sort');
        $list = $db_tags->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 修改
     */
    public function update() {
        $db_tags = DB::name('tag_sort');
        $list = $db_tags->select();
        $map['id'] = $_GET['id'];
        $tag_detail = DB::name('tag_detail')
            ->where($map)
            ->find();
        $this->assign('tag_detail', $tag_detail['name']);
        $this->assign('tag_id', $tag_detail['tag_id']);
        $this->assign('id', $tag_detail['id']);
        $this->assign('list', $list);
        return $this->fetch();
    }
    /**
     * 提交tag
     */
    public function up_tag(){
        $db_tags = DB::name('tag_detail');
        $data['tag_id'] = $_POST['tag_id'];
        $data['name'] = $_POST['name'];

        $res = $db_tags->insert($data);

        if($res){
            return show(1,'添加成功');
        }else{
            return show(0,'添加失败，请重试');
        }
    }

    /**
     * 修改tag
     */
    public function edit_tag(){
        $db_tags = DB::name('tag_detail');
        $data['tag_id'] = $_POST['tag_id'];
        $data['name'] = $_POST['name'];

        $map['id'] = $_POST['id'];
        $res = $db_tags->where($map)->update($data);


        if($res){
            return show(1,'修改成功');
        }else{
            return show(0,'修改失败，请重试');
        }
    }



    /**
     *
     * 删除标签
     */
    public function del_tag(){
        $db_tags = DB::name('tag_detail');

        $data['id'] = $_POST['id'];
        $map['id'] = $data['id'];
        $res = $db_tags->where($map)->delete();
        if($res){
            return show(1,'删除成功');
        }else{
            return show(0,'删除失败，请重试');
        }
    }







}
